/**
 * created by 文阿花, 2021/5/26
 * Copyright (c) 2021, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.light.mytext.websocket;

import android.util.Log;

import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okio.ByteString;

/**
 * 描 述：
 * 作 者：文阿花  2021-05-26 10:53
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */
class EchoWebSocketListener extends WebSocketListener {


    Gson gson = new Gson();
    private disConnectListener listener;

    public EchoWebSocketListener(disConnectListener listener) {
        this.listener = listener;
    }

    @Override
    public void onOpen(WebSocket webSocket, Response response) {
        output("onOpen: ");
    }

    @Override
    public void onMessage(WebSocket webSocket, String text) {
        output("onMessage: " + text);
//        JSONObject msgObj = JSON.parseObject(msg);
//        String action = msgObj.getString("action");
    }

    @Override
    public void onMessage(WebSocket webSocket, ByteString bytes) {
        output("onMessage byteString: " + bytes);
    }

    @Override
    public void onClosing(WebSocket webSocket, int code, String reason) {
        webSocket.close(1000, null);
        output("onClosing: " + code + "/" + reason);
    }

    @Override
    public void onClosed(WebSocket webSocket, int code, String reason) {
        output("onClosed: " + code + "/" + reason);
    }

    @Override

    public void onFailure(WebSocket webSocket, Throwable t, Response response) {
        output("onFailure: " + t.getMessage());
        listener.reconnect();
    }

    private void output(String params) {
        System.out.println(params);
        Log.i("EchoWebSocketListener==",params);
    }

    private String sendData(String sign) {
        String jsonHead = "";
        Map<String, Object> mapHead = new HashMap<>();
        mapHead.put("cmd", "auth2");
        mapHead.put("msg_id", "1");
        mapHead.put("authCode", sign);
        mapHead.put("userId", "111");
        jsonHead = buildRequestParams(mapHead);
        Log.e("TAG", "sendData: " + jsonHead);
        return jsonHead;
    }

    public static String buildRequestParams(Object params) {
        Gson gson = new Gson();
        String jsonStr = gson.toJson(params);
        return jsonStr;
    }

    //定义失败回调的接口
    interface disConnectListener {
        void reconnect();
    }

}
